<?php

namespace Lib\Db;

include __DIR__ . '/sphinxapi.php';

class Sphinx extends \SphinxClient {

	public function __construct($key = 'default') {
		parent::__construct();
		$config = (array) new \Config\Sphinx();
		$config = $config[$key];
		$this->setServer($config['host'], $config['port']);
		$this->setMatchMode (SPH_MATCH_EXTENDED2);
	}

	public function getResultIds($result, $pageCore = null) {
		$resultIds = array();
		if (!empty($result['matches'])) {
			$resultIds = array_keys($result['matches']);
			if ($pageCore) {
				$pageCore->count = $result['total'];
				$pageCore->total_found = $result['total_found'];
				$pageCore->pageCount = ceil($pageCore->count / $pageCore->pageSize);
			}
		}
		return $resultIds;
	}
}